<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="search">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getPerson"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>

      <!-- 用户列表区 -->
      <el-table border stripe :data="personlist">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="用户名" prop="userName"></el-table-column>
        <el-table-column label="姓名" prop="realName"></el-table-column>
        <el-table-column label="密码" prop="password"></el-table-column>
        <el-table-column label="性别" prop="sex"></el-table-column>
        <el-table-column label="电话" prop="phone"></el-table-column>
        <el-table-column label="邮箱" prop="email"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加用户 -->
    <el-dialog
      @close="addDialogClosed"
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="addForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="addForm.realName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <template>
            <el-select v-model="addForm.sex" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户 -->
    <el-dialog
      @close="editDialogClosed"
      title="添加用户"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="70px"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="editForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="editForm.realName"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password"></el-input>
        </el-form-item>
        <!-- <el-form-item label="性别" prop="sex">
          <template>
            <el-select v-model="editForm.sex" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item> -->
        <el-form-item label="电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    //验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^\w+@\w+(\.\w+)+$/;
      if (regEmail.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的邮箱"));
    };
    //验证电话号码的规则
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^1[3|4|5|7|8][0-9]{9}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      //返回一个错误提示
      cb(new Error("请输入合法的手机号码"));
    };
    return {
      //编辑表单数据
      editForm: {},
      //控制编辑对话框的显示与隐藏
      editDialogVisible: false,
      //模糊查询输入框信息
      search: "",
      //求职者数据列表
      personlist: [],
      //控制添加对话框的显示与隐藏
      addDialogVisible: false,
      // 性别的选择
      options: [
        {
          value: "男",
          label: "男",
        },
        {
          value: "女",
          label: "女",
        },
      ],
      value: "",
      //添加表单的验证规则
      addFormRules: {
        userName: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "用户名的长度在3~10个字符之间",
            trigger: "blur",
          },
        ],
        realName: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
          {
            min: 2,
            max: 5,
            message: "用户名的长度在2~5个字符之间",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 16,
            message: "密码的长度在6~16个字符之间",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号码",
            trigger: "blur",
          },
          {
            validator: checkMobile,
            message: "手机号码不正确, 请重新输入",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            validator: checkEmail,
            message: "邮箱格式不正确，请重新输入",
            trigger: "blur",
          },
        ],
      },
      //添加表单数据
      addForm: {
        userName: "",
        realName: "",
        password: "",
        sex: "",
        phone: "",
        email: "",
      },
      //修改表单的验证规则
      editFormRules: {
        userName: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "用户名的长度在3~10个字符之间",
            trigger: "blur",
          },
        ],
        realName: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
          {
            min: 2,
            max: 5,
            message: "用户名的长度在2~5个字符之间",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 16,
            message: "密码的长度在6~16个字符之间",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号码",
            trigger: "blur",
          },
          {
            validator: checkMobile,
            message: "手机号码不正确, 请重新输入",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            validator: checkEmail,
            message: "邮箱格式不正确，请重新输入",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getPersonList();
  },
  methods: {
    async getPersonList() {
      const { data: res } = await this.$http.get("/Admin/users");
      console.log(res);
      if (res.code !== 2002) {
        return this.$message.error("获取求职者信息列表失败！");
      } else {
        this.personlist = res.objData;
      }
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //编辑关闭重置表单
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    addUser() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/Admin/user",
          this.addForm
        );
        console.log(res.code);
        if (res.code !== 2002) {
          this.$message.error("添加求职者信息失败！");
        }
        this.$message.success("添加求职者信息成功！");

        this.addDialogVisible = false;
        this.getPersonList();
      });
    },
    // 编辑用户
    editUser() {
      console.log(this.editForm);
      let {
        id,
        userName,
        realName,
        password,
        phone,
        email,
        sex,
      } = this.editForm;
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        //发起修改用户信息的请求
        const { data: res } = await this.$http.put("Admin/user/", {
          id: id,
          userName: userName,
          realName: realName,
          password: password,
          phone: phone,
          email: email,
          sex: sex,
        });
        if (res.code !== 2002) {
          return this.$message.error("更新用户信息失败！");
        }
        //关闭对话框
        this.editDialogVisible = false;
        console.log(this.editDialogVisible);
        //重新渲染用户列表
        this.getPersonList();
        //更新成功提醒信息
        this.$message.success("更新用户信息成功！");
      });
    },
    //模糊查询
    getPerson() {
      var reg = new RegExp(this.search, "g");
      this.personlist = this.personlist.filter(function (userfile) {
        return reg.test(userfile.userName);
      });
      console.log(this.personlist);
      if (this.search === "") {
        this.getPersonList();
      }
    },
    //删除求职者信息
    async removeUserById(id) {
      let data = {
        id: id,
      };

      //弹框询问用户是否删除
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户，是否继续？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      //如果用户确认删除，则返回值是comfirm
      //如果用户取消删除，则返回值是cancel
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      console.log(JSON.stringify(data));
      //   console.log(data);
      const { data: res } = await this.$http.delete("/Admin/user", {
        data: data,
      });
      console.log(res);
      if (res.code !== 2002) {
        return this.$message.error("删除用户失败！");
      }

      this.$message.success("删除用户成功！");
      this.getPersonList();
    },
    // 用户编辑框显示
    async showEditDialog(id) {
      this.editDialogVisible = true;
      //   console.log(id);
      const { data: res } = await this.$http.get("/Admin/users/" + id);
      console.log("res", res);
      if (res.code !== 2002) {
        return this.$message.error("查询用户信息失败！");
      }
      this.editForm = res.objData;
      //   this.editDialogVisible = true;
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 20px;
}
.el-table {
  margin-top: 10px;
}
.el-select {
  margin-left: -430px;
}
</style>